package uk.ac.lkl.lpp.ui;



//import java.lang.reflect.Constructor;

import java.awt.BorderLayout; //import java.awt.FlowLayout;
import java.awt.Point;

import java.awt.event.*;

import javax.swing.*;

import uk.ac.lkl.common.associator.Associator; //import uk.ac.lkl.common.associator.ui.BasicAssociatorView;
import uk.ac.lkl.common.associator.ui.NewAssociatorView; //import uk.ac.lkl.common.ui.Labelled;
import uk.ac.lkl.common.ui.ViewCreator;
import uk.ac.lkl.common.util.*;
import uk.ac.lkl.lpp.model.ldo.LearningEntity;


/**
 * A view of an Associator.
 * 
 * todo: change name to reflect close tie to associator.
 * 
 * todo: put in general.ui package
 * 
 * @author Darren Pearce
 * @version $Revision: 606 $
 * @version $Date: 2008-02-15 18:08:28 +0000 (Fri, 15 Feb 2008) $
 * 
 */
public class LinkingPanel<S, A, D> extends JPanel {

    private static final long serialVersionUID = 1L;


    private static final JDialog editDialog = new JDialog();


    LearningEntityEditor lee;


    // private JTextField sourceField;

    // private JTextField destinationField;

    private Associator<S, A, D> associator;


    private Converter<String, S> sourceConverter;


    private Converter<String, D> destinationConverter;


    private Class<S> sourceClass;


    private Class<D> destinationClass;


    private JButton createSourceButton;


    private JButton createDestinationButton;


    public LinkingPanel(Class<S> sourceClass, Class<D> destinationClass,
            Class<A> annotationClass, Associator<S, A, D> associator,
            ViewCreator<S> sourceViewCreator,
            ViewCreator<D> destinationViewCreator) {
        this(
             sourceClass,
             destinationClass,
             annotationClass,
             associator,
             null,
             sourceViewCreator,
             null,
             destinationViewCreator);
    }


    /**
     * 
     * Note: either of the converter args can be null. Both null is equivalent
     * to constructor above.
     * 
     * @param sourceClass
     * @param destinationClass
     * @param associator
     * @param sourceConverter
     * @param sourceViewCreator
     * @param destinationConverter
     * @param destinationViewCreator
     */
    public LinkingPanel(Class<S> sourceClass, Class<D> destinationClass,
            Class<A> annotationClass, Associator<S, A, D> associator,
            Converter<String, S> sourceConverter,
            ViewCreator<S> sourceViewCreator,
            Converter<String, D> destinationConverter,
            ViewCreator<D> destinationViewCreator) {

        if (sourceConverter == null)
            sourceConverter = new ConstructorBasedConverter<String, S>(
                    String.class, sourceClass);

        if (destinationConverter == null)
            destinationConverter = new ConstructorBasedConverter<String, D>(
                    String.class, destinationClass);

        this.sourceClass = sourceClass;
        this.destinationClass = destinationClass;
        this.associator = associator;
        this.sourceConverter = sourceConverter;
        this.destinationConverter = destinationConverter;

        // BasicAssociatorView<S, A, D> view = new BasicAssociatorView<S, A, D>(
        // associator,
        // sourceViewCreator,
        // destinationViewCreator);

        NewAssociatorView view = new NewAssociatorView(associator,
                sourceViewCreator, destinationViewCreator);

        setLayout(new BorderLayout());

        add(view, BorderLayout.CENTER);

        // hack - do this more neatly
        JPanel createSourcePanel = new JPanel();
        // sourceField = new JTextField(10);
        // createSourcePanel.add(sourceField);

        createSourceButton = new JButton();
        setCreateSourceButtonLabel("Source");

        createSourceButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                createSource();
                // LearningEntityEditor
            }
        });

        createSourcePanel.add(createSourceButton);

        // hack - do this more neatly
        JPanel createDestinationPanel = new JPanel();
        // destinationField = new JTextField(10);
        // createDestinationPanel.add(destinationField);

        createDestinationButton = new JButton();
        setCreateDestinationButtonLabel("Destination");

        createDestinationButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                createDestination();
                // LearningEntityEditor
            }
        });

        createDestinationPanel.add(createDestinationButton);

        JPanel createPanel = new JPanel();
        createPanel.add(createSourcePanel);
        createPanel.add(createDestinationPanel);
        add(createPanel, BorderLayout.NORTH);

    }


    public void setCreateSourceButtonLabel(String text) {
        createSourceButton.setText("Create " + text);
    }


    public void setCreateDestinationButtonLabel(String text) {
        createDestinationButton.setText("Create " + text);
    }


    public Class<S> getSourceClass() {
        return sourceClass;
    }


    public Class<D> getDestinationClass() {
        return destinationClass;
    }


    /**
     * create a new entity of the source class
     * 
     */
    private void createSource() {
        String sourceName = "New";

        S sourceElement = sourceConverter.convert(sourceName);
        associator.getLeftList().add(sourceElement);
        LearningEntityEditor.getEditorDialog(
                                             (LearningEntity) sourceElement,
                                             createSourceButton
                                                     .getLocationOnScreen())
                .setVisible(true);

    }


    /**
     * create a new entity of the destination class
     * 
     */
    private void createDestination() {
        String destinationName = "New";
        D destinationElement = destinationConverter.convert(destinationName);
        associator.getRightList().add(destinationElement);
        LearningEntityEditor
                .getEditorDialog(
                                 (LearningEntity) destinationElement,
                                 createDestinationButton.getLocationOnScreen())
                .setVisible(true);
    }

}
